Conditions | 1 |
Paths | 1 |
Total Lines | 155 |
Lines | 155 |
Ratio | 100 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | View Code Duplication | var chai = require('chai'); |
|
13 | describe('GraphQL Beer (mutations)', function() { |
||
14 | |||
15 | beforeEach(function(done){ |
||
16 | BreweryData.forEach(function (aBeer) { |
||
17 | var newBrewery = new Brewery(aBeer); |
||
18 | newBrewery.save(); |
||
19 | }); |
||
20 | BeerData.forEach(function (aBeer) { |
||
21 | var newBeer = new Beer(aBeer); |
||
22 | newBeer.save(); |
||
23 | }); |
||
24 | done(); |
||
25 | }); |
||
26 | |||
27 | afterEach(function(done){ |
||
28 | Beer.collection.drop(); |
||
29 | Brewery.collection.drop(); |
||
30 | done(); |
||
31 | }); |
||
32 | |||
33 | it('should add a new beer', function(done) { |
||
34 | |||
35 | var newBrewery = new Brewery({ |
||
36 | _id: 'test_brewery', |
||
37 | name: 'Test Brewery', |
||
38 | location: 'Beerland' |
||
39 | }); |
||
40 | |||
41 | newBrewery.save(); |
||
42 | |||
43 | var graphqlQuery = ` |
||
44 | mutation { |
||
45 | addBeer(data: {name: "Beer Test Triple", brewery: "test_brewery", alcohol: 8.5}) |
||
46 | } |
||
47 | `; |
||
48 | |||
49 | chai.request(server) |
||
50 | .post('/graphql') |
||
51 | .send({query: graphqlQuery}) |
||
52 | .end(function(err, res){ |
||
53 | res.should.have.status(200); |
||
54 | res.should.be.json; |
||
55 | res.body.data.addBeer.should.be.true; |
||
56 | }); |
||
57 | |||
58 | // find this new beer |
||
59 | var graphqlQuery = ` |
||
60 | { |
||
61 | beers(brewery: "test_brewery") { |
||
62 | name |
||
63 | brewery { |
||
64 | name |
||
65 | } |
||
66 | alcohol |
||
67 | } |
||
68 | }`; |
||
69 | |||
70 | chai.request(server) |
||
71 | .post('/graphql') |
||
72 | .send({query: graphqlQuery}) |
||
73 | .end(function(err, res){ |
||
74 | res.should.have.status(200); |
||
75 | res.body['data']['beers'][0]['name'].should.equal('Beer Test Triple'); |
||
76 | res.body['data']['beers'][0]['brewery'].name.should.equal('Test Brewery'); |
||
77 | res.body['data']['beers'][0]['alcohol'].should.equal(8.5); |
||
78 | done(); |
||
79 | }); |
||
80 | }); |
||
81 | |||
82 | it('should delete a SINGLE beer', function(done) { |
||
83 | var newBeer = new Beer({ |
||
84 | name: 'Custom Beer', |
||
85 | brewery: 'Custom Brewery', |
||
86 | alcohol: 9.5, |
||
87 | description: 'hummmmmmm' |
||
88 | }); |
||
89 | |||
90 | newBeer.save(function(err, data) { |
||
91 | |||
92 | var graphqlQuery = ` |
||
93 | mutation { |
||
94 | removeBeer(_id: "${data._id}") { |
||
95 | name |
||
96 | } |
||
97 | }`; |
||
98 | |||
99 | chai.request(server) |
||
100 | .post('/graphql') |
||
101 | .send({query: graphqlQuery}) |
||
102 | .end(function(err, res){ |
||
103 | res.should.have.status(200); |
||
104 | res.should.be.json; |
||
105 | res.body.should.be.an('object'); |
||
106 | res.body.data.removeBeer.name.should.equal('Custom Beer'); |
||
107 | done(); |
||
108 | }); |
||
109 | |||
110 | // try to retrieve the deleted beer |
||
111 | var graphqlQuery = ` |
||
112 | { |
||
113 | beer(id: "${data._id}") { |
||
114 | name |
||
115 | } |
||
116 | }`; |
||
117 | |||
118 | chai.request(server) |
||
119 | .post('/graphql') |
||
120 | .send({query: graphqlQuery}) |
||
121 | .end(function(err, res){ |
||
122 | res.should.have.status(200); |
||
123 | res.should.be.json; |
||
124 | res.body.should.be.an('object'); |
||
125 | res.body.data.beer.should.be.null; |
||
126 | done(); |
||
127 | }); |
||
128 | }); |
||
129 | }); |
||
130 | |||
131 | it('should delete all beers', function(done) { |
||
132 | |||
133 | var graphqlQuery = ` |
||
134 | mutation { |
||
135 | removeAllBeers |
||
136 | } |
||
137 | `; |
||
138 | |||
139 | chai.request(server) |
||
140 | .post('/graphql') |
||
141 | .send({query: graphqlQuery}) |
||
142 | .end(function(err, res){ |
||
143 | res.should.have.status(200); |
||
144 | res.should.be.json; |
||
145 | res.body.data.removeAllBeers.should.be.true; |
||
146 | }); |
||
147 | |||
148 | // retrieve all beers |
||
149 | var graphqlQuery = ` |
||
150 | { |
||
151 | beers { |
||
152 | name |
||
153 | } |
||
154 | }`; |
||
155 | |||
156 | chai.request(server) |
||
157 | .post('/graphql') |
||
158 | .send({query: graphqlQuery}) |
||
159 | .end(function(err, res){ |
||
160 | res.should.have.status(200); |
||
161 | res.body.data.beers.should.be.an('array'); |
||
162 | res.body.data.beers.should.have.lengthOf(0); |
||
163 | done(); |
||
164 | }); |
||
165 | }); |
||
166 | |||
167 | }); |
||
168 |